#include "cairdim.h"
#include "cmd_parse/json_device_object.h"
CAirDim::CAirDim(QObject *parent) :
    CKnxControl(parent)
{
    _mode_id = -1;
    _wind_id = -1;
    _curent_temp = 16;
}

void CAirDim::setKnxObject(void *jOBJECT)
{
    //表达式状态
    _onValue = "1"; _offValue = "0";
    _min = 16; _max = 30;

    Json_DEVICE_OBJECT *object = (Json_DEVICE_OBJECT *)jOBJECT;

    QStringList arrys;
    object->_exp = object->_exp.trimmed();
    if(!object->_exp.isEmpty())
    {
        arrys = object->_exp.split(";");
        if(arrys.size()!=0)
        {
            for(int i = 0;i<arrys.size();i++)
            {
                if (i == 0)
                {
                    _min = arrys.at(i).toFloat();
                }
                else if (i == 1)
                {
                    _max = arrys.at(i).toFloat();
                }
                else if (i == 2)
                {
                    _mode_id = arrys.at(i).toInt();
                }
                else if (i == 3)
                {
                    _wind_id = arrys.at(i).toInt();
                }
            }
        }
    }

    //发送值
    arrys = object->_send.split(";");
    if(arrys.size()>2)
    {
        QString sonArray = arrys.at(0);
        _sendOnArray = sonArray.split(",");

        QString soffArray = arrys.at(1);
        _sendOffArray = soffArray.split(",");

        QString sValueArray = arrys.at(2);
        _sendValueArray = sValueArray.split(",");
    }

    //反馈值
    arrys = object->_feedback.split(";");
    if(arrys.size()>1)
    {
        QStringList feedList = arrys.at(0).split(":");
        if(feedList.size() >= 3)
        {
            _feedbackAddress = feedList.at(0);
            _feedbackDataType =  (DATA_TYPE)feedList.at(1).toUInt();
            _feedbackValueDefine =  (DATA_DEFINE)feedList.at(2).toUInt();
        }

        feedList = arrys.at(1).split(":");
        if(feedList.size() >= 3)
        {
           _feedbackDimAddress = feedList.at(0);
           _feedbackDimType =  (DATA_TYPE)feedList.at(1).toUInt();
           _feedbackDimDefine =  (DATA_DEFINE)feedList.at(2).toUInt();
        }
    }
}

QString CAirDim::valueCheck(QString action, QString value)
{
    if(action == "value")
    {
        float nv = value.toFloat();
        if(nv<_min)
            nv = _min;

        if(nv>_max)
            nv = _max;

        _curent_temp = (int)nv;
        return QString("%1").arg(nv);
    }

    return value;
}

void CAirDim::readKnxAction()
{
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    CExecStruct *s = new CExecStruct();
    s->address = _feedbackAddress;
    eThread->execList.append(s);

    s = new CExecStruct();
    s->address = _feedbackDimAddress;
    eThread->execList.append(s);

    eThread->start();
}

void CAirDim::execKnxAction(QString action,QString value)
{
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));


    float onOffValue;
    QStringList *sendArray = &_sendOnArray;

    if (action == "off") {
        sendArray = &_sendOffArray;
    }
    else if(action == "value") {
        sendArray = &_sendValueArray;
        onOffValue = value.toFloat();
    }

    QString sendAddress,sendValue;
    DATA_TYPE sendDataType;
    DATA_DEFINE sendValueDefine;

    for (int i=0; i<sendArray->size(); i++)
    {
        QString dev = sendArray->at(i);
        QStringList devArray = dev.split(":");

        if (devArray.size() >= 3) {

            sendAddress = devArray.at(0);

            if (sendAddress == "msleep") {
                CExecStruct *s = new CExecStruct();
                s->address = dev;

                eThread->execList.append(s);
            }
            else
            {
                sendDataType = (DATA_TYPE)devArray.at(1).toUInt();
                sendValueDefine = (DATA_DEFINE)devArray.at(2).toUInt();
                if(action != "value")
                    sendValue = devArray.at(3);
                else
                    sendValue = QString::number(onOffValue);

                if(!sendAddress.isEmpty())
                {
                    int data_len = 0;
                    memset(_data_buf,0,sizeof(_data_buf));

                    if((data_len = DEVICE_OBJECT::KnxQStringToData(sendValue,sendDataType,sendValueDefine,(_u8 *)_data_buf)) == -1)
                    {
                        qDebug() << "SetDev ret='-1' msg='error data args'" ;
                        continue ;
                    }

                    CExecStruct *s = new CExecStruct();
                    s->address = sendAddress;
                    s->type = sendDataType;
                    s->data = QByteArray((const char *)_data_buf,data_len);

                    eThread->execList.append(s);
                }
            }
        }

    }

    eThread->start();
}

QString CAirDim::actionFromStatus(QString address,QString value)
{
    if(address == _feedbackAddress)
    {
        if(value == _onValue)
            return "on";
        else
            return "off";
    }
    else
        return "value";
}

void CAirDim::execDeviceAction(QString action,QString value,QString unit)
{
    if(action == "switch")
    {
        if(value == "1")
        {
            execKnxAction("on",value);
        }
        else
        {
            execKnxAction("off",value);
        }
    }
    else if(action == "temp")
    {
        int setv = 0;

        if(unit.startsWith("="))
        {
            _curent_temp = parseNumberFromString(value);
            execKnxAction("value",value);
        }
        else if(unit.startsWith("+"))
        {
            setv = parseNumberFromString(value);
            _curent_temp += setv;
            if(_curent_temp>_max)
                _curent_temp = _max;

            execKnxAction("value",QString("%1").arg(_curent_temp));
        }
        else if(unit.startsWith("-"))
        {
            setv = parseNumberFromString(value);
            _curent_temp -= setv;
            if(_curent_temp < _min)
                _curent_temp = _min;

            execKnxAction("value",QString("%1").arg(_curent_temp));
        }
    }

}
